package com.bruce.shop.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author BruceXia
 * @version 1.0.0
 * @description:
 * @date 2022/8/3121:53
 */
@WebFilter(filterName = "Filter01_Encording",value = "/*")
public class Filter01_Encording implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        //类型转换
        HttpServletRequest request= (HttpServletRequest) req;
        HttpServletResponse respone= (HttpServletResponse) resp;
        String getURL=request.getRequestURI();

         //除了html，css和js其他都要设置
        if (!(getURL.endsWith(".html")||getURL.endsWith(".css")||getURL.endsWith("js"))) {
            request.setCharacterEncoding("utf-8");
            respone.setContentType("text/html;charset=utf-8");
        }

        //放行
        chain.doFilter(request, respone);





    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
